import utils from '../../utils/util.js';

module.exports = {
    methods: {
        wechatH5Share(title, desc, icon) {
            if (!utils.isWechat()) {
                return;
            }
            let link = window.location.href;
            // #ifdef H5
            this.$u.api.TemplateOne.Jssdk({
                path: link,
            }).then(data => {
                let jsapiConfig = data.data;
                window.wx1.config({
                    debug: jsapiConfig.debug,
                    appId: jsapiConfig.appId,
                    timestamp: jsapiConfig.timestamp,
                    nonceStr: jsapiConfig.nonceStr,
                    signature: jsapiConfig.signature,
                    jsApiList: jsapiConfig.jsApiList
                });

                if (typeof title === 'undefined') {
                    title = data.share.title;
                }
                if (typeof desc === 'undefined') {
                    desc = data.share.desc;
                }
                if (typeof icon === 'undefined') {
                    icon = data.share.icon;
                }

                window.wx1.ready(() => {
                    window.wx1.updateAppMessageShareData({
                        title: title,
                        desc: desc,
                        link: link,
                        imgUrl: icon,
                        success: () => { }
                    });
                    window.wx1.updateTimelineShareData({
                        title: title,
                        link: link,
                        imgUrl: icon,
                        success: () => { }
                    })
                });
            })
            // #endif
        }
    }
}